{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e272d4d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initialize hikyuu_2.6.8_202510162356_RELEASE_macosx_arm64 ...\n",
      "2025-10-17 00:14:03.128 [HKU-I] - current python version: 3.12.7 (main.cpp:72)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-10-17 00:14:05,037 [INFO] runing in interactive session [<module>] (/Users/fasiondog/workspace/hikyuu/hikyuu/__init__.py:110) [hikyuu::hku_info]\n",
      "2025-10-17 00:14:05,038 [INFO] running in jupyter [<module>] (/Users/fasiondog/workspace/hikyuu/hikyuu/__init__.py:117) [hikyuu::hku_info]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-10-17 00:14:05.042 [HKU-I] - Plugin path: /Users/fasiondog/workspace/hikyuu/hikyuu/plugin (StockManager.cpp:113)\n",
      "2025-10-17 00:14:06.773 [HKU-I] - Using CLICKHOUSE BaseInfoDriver (BaseInfoDriver.cpp:57)\n",
      "2025-10-17 00:14:06.793 [HKU-I] - 加载市场信息…… (StockManager.cpp:704)\n",
      "2025-10-17 00:14:06.802 [HKU-I] - 加载证券类型信息…… (StockManager.cpp:722)\n",
      "2025-10-17 00:14:06.808 [HKU-I] - 加载证券信息…… (StockManager.cpp:602)\n",
      "2025-10-17 00:14:06.896 [HKU-I] - 加载权息数据…… (StockManager.cpp:740)\n",
      "2025-10-17 00:14:07.062 [HKU-I] - 加载板块信息…… (StockManager.cpp:182)\n",
      "2025-10-17 00:14:07.308 [HKU-I] - 加载K线数据…… (StockManager.cpp:186)\n",
      "2025-10-17 00:14:07.308 [HKU-I] - 预加载 day K线数据至缓存 (最大数量: 100000)! (StockManager.cpp:231)\n",
      "2025-10-17 00:14:07.309 [HKU-I] - 0.53 秒数据加载完毕. (StockManager.cpp:193)\n"
     ]
    }
   ],
   "source": [
    "from hikyuu.interactive import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b700b002",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建两个因子 ma20, ma60\n",
    "ma20 = MA(CLOSE(), 20)\n",
    "ma20.name = 'MA20'\n",
    "\n",
    "ma60 = MA(CLOSE(), 60)\n",
    "ma60.name = 'MA60'\n",
    "\n",
    "# 指定证券列表\n",
    "stks = [s for s in blocka]\n",
    "\n",
    "# 指定查询范围，并创建一个等权组合的 MF\n",
    "query = Query(Datetime(20150101), Datetime(20251017))\n",
    "mf = MF_EqualWeight([ma20, ma60], stks, query, ref_stk=sm[\"sh000001\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "75f0f14e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      证券代码  证券名称        score\n",
      "0     SH600519  贵州茅台  1460.640867\n",
      "1     SH603444    吉比特   500.309125\n",
      "2     SZ002371  北方华创   395.552042\n",
      "3     BJ920982  锦波生物   300.004475\n",
      "4     SH605499  东鹏饮料   295.006675\n",
      "...        ...       ...          ...\n",
      "3974  BJ872808  曙光数创          NaN\n",
      "3975  BJ873527    夜光明          NaN\n",
      "3976  BJ832471  美邦科技          NaN\n",
      "3977  BJ430478  峆一药业          NaN\n",
      "3978  SZ000675  ST 银 山          NaN\n",
      "\n",
      "[3979 rows x 3 columns]\n"
     ]
    }
   ],
   "source": [
    "# 没有标准化时，获取合成后某日的评分列表\n",
    "scores = mf.get_scores(Datetime(20251016))\n",
    "print(scores.to_df())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b0f90d40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      证券代码  证券名称      score\n",
      "0     SH600519  贵州茅台  43.150291\n",
      "1     SH603444    吉比特  14.382820\n",
      "2     SZ002371  北方华创  11.265517\n",
      "3     BJ920982  锦波生物   8.427688\n",
      "4     SH605499  东鹏饮料   8.272032\n",
      "...        ...       ...        ...\n",
      "3974  BJ872808  曙光数创        NaN\n",
      "3975  BJ873527    夜光明        NaN\n",
      "3976  BJ832471  美邦科技        NaN\n",
      "3977  BJ430478  峆一药业        NaN\n",
      "3978  SZ000675  ST 银 山        NaN\n",
      "\n",
      "[3979 rows x 3 columns]\n"
     ]
    }
   ],
   "source": [
    "# 添加全局标准化\n",
    "mf.set_normalize(NORM_Zscore())\n",
    "scores = mf.get_scores(Datetime(20251016))\n",
    "print(scores.to_df())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cd8406ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      证券代码  证券名称      score\n",
      "0     SH600519  贵州茅台  43.114519\n",
      "1     SH603444    吉比特  14.403012\n",
      "2     SZ002371  北方华创  11.271072\n",
      "3     BJ920982  锦波生物   8.407414\n",
      "4     SH605499  东鹏饮料   8.233678\n",
      "...        ...       ...        ...\n",
      "3974  BJ872808  曙光数创        NaN\n",
      "3975  BJ873527    夜光明        NaN\n",
      "3976  BJ832471  美邦科技        NaN\n",
      "3977  BJ430478  峆一药业        NaN\n",
      "3978  BJ832566    梓橦宫        NaN\n",
      "\n",
      "[3979 rows x 3 columns]\n"
     ]
    }
   ],
   "source": [
    "# 为 ma20 添加行业中性化以及市场中性化(即按市值风格因子中性化)\n",
    "mf.add_special_normalize(\"MA20\", NORM_Zscore(), category=\"行业板块\", style_inds=[LOG(CLOSE()*LIUTONGPAN())])\n",
    "scores = mf.get_scores(Datetime(20251016))\n",
    "print(scores.to_df())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
